<template>
  <el-dialog
    title="分配角色"
    :visible="isShow"
    width="600px"
    @close="closeEvent"
  >
    <h1>{{ roleIds }}</h1>
    <!-- v-model   :value   @input
     el-checkbox
          v-model:双向绑定的值，如果是非数组选中后就是true,取消选中就是false,如果是数组，选中后数组就添加当前项的label的值
          label:选中该项后的值
    el-checkbox-group:一组多选框
          v-model
    -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <div>
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  data() {
    return {
      //   isShow: false
      roleIds: [],
      list: [],
      id: ''
    }
  },
  methods: {
    closeEvent() {
      this.$emit('update:isShow', false)
      //   清空勾选
      this.roleIds = []
    },
    async getData() {
      if (this.list.length === 0) {
        const res = await sysRole({ page: 1, pagesize: 999 })
        this.list = res.data.rows
        console.log('角色列表', res)
      }
    },
    // 获取详情用户信息包括角色信息
    async getUserRoleIds(id) {
      this.id = id
      const res = await sysUser(id)
      this.roleIds = res.data.roleIds || []
      console.log('用户详情', res)
    },
    async submit() {
      await sysUserAssignRoles({
        id: this.id,
        roleIds: this.roleIds
      })
      this.$message.success('分配角色成功')
      this.closeEvent()
    }
  }
}
</script>
<style></style>
